package com.jt.blog.service;/**
 * Created by Administrator on 2016/8/30.
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Throwables;
import com.jt.blog.common.model.Page;
import com.jt.blog.common.model.Response;
import com.jt.blog.mapper.BlogMapper;
import com.jt.blog.model.Blog;
import com.jt.blog.model.BlogStatistics;
import com.jt.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangtao
 * @create 2016-08-30 11
 **/
@Service
public class BlogReadServiceImpl extends AbstractBaseService<Blog> implements BlogReadService {

    @Autowired
    BlogStatisticsService blogStatisticsService;

    public Response<Page<Blog>> page(Page<Blog> page,BlogQuery query) {
        Response<Page<Blog>> response = new Response<Page<Blog>>();
        try{
            PageHelper.startPage(page.getPageNo(),page.getPageSize());
            Example example = new Example(Blog.class);
            Example.Criteria criteria = example.createCriteria();
            if(null!=query){
                if(null!=query.getCategoryId()){
                    criteria.andEqualTo("categoryId",query.getCategoryId());
                }
                if(null!=query.getBeginTime()){
                    criteria.andBetween("createTime",query.getBeginTime(),query.getEndTime());
                }
            }
            List<Blog> blogs = mapper.selectByExample(example);
            wrapBlogStatistics(blogs);
            PageInfo<Blog> temp = new PageInfo<Blog>(blogs);
            page.setData(temp.getList());
            page.setTotal(temp.getTotal());
            page.setPageMax(temp.getLastPage());
            response.setResult(page);
        }catch (Exception e){
            logger.error("blog page fail, params = {}, query = {}", query, Throwables.getStackTraceAsString(e));
            response.setError("blog.page.fail");
        }
        return response;
    }

    public Response<Blog> get(Long id) {
        Response<Blog> response = new Response<Blog>();
        try {
            Blog blog = mapper.selectByPrimaryKey(id);
            BlogStatistics blogStatistics = blogStatisticsService.get(id);
            blog.setBlogStatistics(blogStatistics);
            response.setResult(blog);
        } catch (Exception e){
            logger.error("blog get fail, id = {}, error = {}", id, Throwables.getStackTraceAsString(e));
            response.setError("blog.get.fail");
        }
        return response;
    }

    public Response<Map<String, Blog>> getPrevAndNextBlog(Long id) {
        Response<Map<String, Blog>> response = new Response<Map<String, Blog>>();
        Map<String, Blog> map = new HashMap<String, Blog>();
        try {
            BlogMapper blogMapper = (BlogMapper) mapper;
            List<Blog> blogs = blogMapper.getPrevAndNextBlog(id);
            if(blogs!=null&&blogs.size()>0){
                for(Blog temp : blogs){
                    Long tempId = temp.getId();
                    if(tempId>id){
                        map.put("next",temp);
                    }else{
                        map.put("prev",temp);
                    }
                }
            }
            response.setResult(map);
        } catch (Exception e){
            logger.error("blog get fail, id = {}, error = {}", id, Throwables.getStackTraceAsString(e));
            response.setError("blog.get.fail");
        }
        return response;
    }

    private void wrapBlogStatistics(List<Blog> blogs){
        for(Blog blog : blogs){
            Long blogId = blog.getId();
            BlogStatistics blogStatistics = blogStatisticsService.get(blogId);
            blog.setBlogStatistics(blogStatistics);
        }
    }

}
